package jmine.tec.database.pack;

import static jmine.tec.database.pack.PackingMessages.IO_EXCEPTION;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import jmine.tec.utils.io.ReversibleOutputStream;

/**
 * Elemento de um pacote de arquivos
 * 
 * @author lundberg
 */
public class PackElement {

    private final String fileCode;

    private final ReversibleOutputStream input;

    private final String[] path;

    /**
     * Construtor
     * 
     * @param fileCode fileCode
     * @param input input
     * @param path path
     */
    public PackElement(String fileCode, ReversibleOutputStream input, String... path) {
        this.fileCode = fileCode;
        this.input = input;
        this.path = path;
    }

    /**
     * @return the fileCode
     */
    public String getFileCode() {
        return this.fileCode;
    }

    /**
     * @return the input
     * @throws PackException PackException
     */
    public InputStream getInput() throws PackException {
        try {
            return this.input.reverse();
        } catch (IOException e) {
            throw new PackException(IO_EXCEPTION, e);
        }
    }

    /**
     * @return the path
     */
    public String[] getPath() {
        return this.path;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof PackElement)) {
            return false;
        }
        PackElement o = (PackElement) obj;
        return Arrays.equals(this.path, o.path);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.path);
    }
}
